package com.quyeying.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quyeying.entity.TblMerchantEntity;
import com.quyeying.service.TblMerchantService;
import com.quyeying.utils.JwtUtil;
import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ResultEnum;
import org.springframework.web.bind.annotation.*;
import com.quyeying.service.TblProductCategoryService;
import com.quyeying.entity.TblProductCategoryEntity;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* <p>
* tbl_product_category 前端控制器
* </p>
* @author skx
* @since 2022-10-18
*/
@Slf4j
@RestController
@RequestMapping("productCategoryEntity")
public class TblProductCategoryController {

    @Resource
    private TblProductCategoryService tblProductCategoryService;
    @Resource
    private TblMerchantService tblMerchantService;

    /**
    * 新增
    **/
    @PostMapping("/add")
    public R add(@RequestBody TblProductCategoryEntity tblProductCategoryEntity, HttpServletRequest request){
        try {
            String authorization = request.getHeader("Authorization");
            String userName = JwtUtil.getUserName(authorization);
            TblMerchantEntity merchantInfo = tblMerchantService.getMerchantInfo(userName);
            tblProductCategoryEntity.setMerchantId(merchantInfo.getId());
            return tblProductCategoryService.add(tblProductCategoryEntity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            Throwable cause = e.getCause();
            if(cause instanceof java.sql.SQLIntegrityConstraintViolationException) {
                throw new RuntimeException("分类名称已存在");
            }
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 删除
    **/
    @DeleteMapping("/del")
    public R delete(Long id){
        try {
            return tblProductCategoryService.delete(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 更新
    **/
    @PostMapping("/update")
    public R update(@RequestBody TblProductCategoryEntity tblProductCategoryEntity){
        try {
            return tblProductCategoryService.updateData(tblProductCategoryEntity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            log.error(e.getMessage(), e);
            Throwable cause = e.getCause();
            if(cause instanceof java.sql.SQLIntegrityConstraintViolationException) {
                throw new RuntimeException("分类名称已存在");
            }
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 分页查询
    **/
    @GetMapping("/findListByPage")
    public R findListByPage(@RequestParam Integer current,
                            @RequestParam Integer size,
                            @RequestParam Long parentId,HttpServletRequest request){
        try {
            String authorization = request.getHeader("Authorization");
            String userName = JwtUtil.getUserName(authorization);
            TblMerchantEntity merchantInfo = tblMerchantService.getMerchantInfo(userName);
            return tblProductCategoryService.findListByPage(current, size, parentId,merchantInfo.getId());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * id查询
    **/
    @GetMapping("/findById")
    public R findById(Long id){
        try {
            return tblProductCategoryService.findById(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取树状分类
     */
    @GetMapping("/getCategoryTree")
    public R getCategoryTree(HttpServletRequest request){
        String authorization = request.getHeader("Authorization");
        String userName = JwtUtil.getUserName(authorization);
        TblMerchantEntity merchantInfo = tblMerchantService.getMerchantInfo(userName);
        QueryWrapper<TblProductCategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("merchant_id", merchantInfo.getId()).eq("parent_id", 0).orderByAsc("sort");
        List<TblProductCategoryEntity> list = tblProductCategoryService.list(wrapper);
        for (TblProductCategoryEntity item:list
             ) {
            wrapper.clear();
            wrapper.eq("merchant_id", merchantInfo.getId()).eq("parent_id", item.getId()).orderByAsc("sort");
            List<TblProductCategoryEntity> twoList = tblProductCategoryService.list(wrapper);
            item.setChildren(twoList);
            for (TblProductCategoryEntity itemTwo:item.getChildren()
            ) {
                wrapper.clear();
                wrapper.eq("merchant_id", merchantInfo.getId()).eq("parent_id", itemTwo.getId()).orderByAsc("sort");
                List<TblProductCategoryEntity> threeList = tblProductCategoryService.list(wrapper);
                itemTwo.setChildren(threeList);
            }
        }
        return R.success(list);
    }
}
